package com.leetcode.用两个栈实现队列;

import java.util.Stack;

/**
 * <p>
 * 参考：https://www.nowcoder.com/practice/54275ddae22f475981afa2244dd448c6?tpId=13&tqId=11158&rp=1&ru=%2Fta%2Fcoding-interviews&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking&tab=answerKey
 * </p>
 *
 * @author: zhu.chen
 * @date: 2021/3/15
 * @version: v1.0.0
 */
public class Main {

    /**
     * 算法要点：
     * stack：先进后出。
     * queue：先进先出。
     *
     * stack1：
     * |---------|
     * | a b c d |
     * |---------|
     *
     *     ||
     *
     * stack2：
     * |---------|
     * | d c b a |
     * |---------|
     *
     * ========> 最后从stack2取出为a b c d
     */

    Stack<Integer> stack1 = new Stack<Integer>();

    Stack<Integer> stack2 = new Stack<Integer>();

    public void push(int node) {
        stack1.push(node);
    }

    public int pop() {
        if (stack1.empty() && stack2.empty()) {
            throw new RuntimeException("Queue is Empty!");
        }
        if (stack2.empty()) {
            while (!stack1.empty()) {
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.push(3);
        main.push(4);
        main.push(5);
        System.out.println(main.pop());
    }

}
